package 设计模式;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class Singleton2 {
    private static Singleton2 instance;
    private String name;
    private Singleton2() {
    }
    @SneakyThrows
    public static Singleton2 getInstance() {
        if (instance == null) {
            synchronized (Singleton2.class) {
                if (instance == null) {
                    instance = new Singleton2();
                    Thread.sleep(5000);
                    instance.setName("张三疯");
                }
            }
        }
        return instance;
    }
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            Singleton2 instance = Singleton2.getInstance();
            log.info("instance:{}", instance);
        };
        new Thread(runnable, "t1").start();
        Thread.sleep(1000);
        new Thread(runnable, "t2").start();
    }
}

